package com.mo.service.impl;

import com.mo.dao.SysUserDao;
import com.mo.dao.SysUserRoleDao;
import com.mo.pojo.SysUser;
import com.mo.service.SysUserService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserDao sysUserDao;

//    private SysUserRoleDao sysUserRoleDao;


    @Override
    public List<SysUser> findUser(SysUser user) {
        List<SysUser> list=sysUserDao.selectUser(user);
        return list;
    }

    @Override
    public SysUser findById(long uCode) {

        SysUser user=sysUserDao.findByuCode(uCode);


        return user;
    }

    @Override
    public int saveUser(SysUser user) {
       int rows= sysUserDao.insertUser(user);
        return rows;
    }

    @Override
    public int updateUser(SysUser user) {
        int rows=sysUserDao.updateUser(user);
        return rows;
    }

    @Override
    public int deleteById(Long uCode) {
       int rows =sysUserDao.deleteByuCode(uCode);
        return rows;
    }

    @Override
    public int updatePassword(String sourcePassword, String newPassword, String confirmPassword) {
        //1.参数校验
        if(sourcePassword==null||"".equals(sourcePassword))
            throw new IllegalArgumentException("原密码不能为空");
        if(newPassword==null||"".equals(newPassword))
            throw new IllegalArgumentException("新密码不能为空");
        if(!newPassword.equals(confirmPassword))
            throw new IllegalArgumentException("两次输入的新密码不一致");
        //获取登录用户,并判定登录用户的密码和用户输入的原密码是否一致
        SysUser sysUser=
                (SysUser) SecurityUtils.getSubject().getPrincipal();

        if(!sysUser.getUCode().equals(sysUser.getUPwd()))
            //MD5加密，相同内容加密结果也相同
            throw new IllegalArgumentException("原密码不正确");

        //3.更新密码
        int rows=sysUserDao.updatePassword(newPassword,
                sysUser.getUCode());
        return rows;

    }
}
